package com.itheima.service.imp;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {

//    CheckItemServiceImpl(){
//        System.out.println("CheckItemServiceImpl........................init");
//    }
    @Autowired
  private CheckItemDao checkItemDao;
    @Override
    public void add(CheckItem checkItem) {
        System.out.println("service");
        checkItemDao.add(checkItem);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        /*
        * 分页查询所有的
        * */
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        Page<CheckItem> page = checkItemDao.findPage(queryString);
        PageResult pageResult = new PageResult(page.getTotal(), page.getResult());
        return pageResult;
    }

    @Override
    /*删除操作检查项*/
    public Boolean deleteById(Integer id) {
//        判断是否与检查组关联，如果与检查组关联则不能删除

        long result = checkItemDao.findCountByCheckId(id);
        if (result==0){
            checkItemDao.delete(id);
            return true;
        }

        return false;
       /*如果bool为true 表示该检查项不能删除，如果为false则可以删除*/
    }

    @Override
    public CheckItem show(Integer id) {
        CheckItem checkItem = checkItemDao.show(id);
        return checkItem;
    }

    @Override
    public Boolean modify(CheckItem checkItem) {
        try {
            checkItemDao.modify(checkItem);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public List<CheckItem> findAll() throws Exception{
        List<CheckItem> checkItems = checkItemDao.findAll();
        return checkItems;
    }


}
